<?php
// 本类由系统自动生成，仅供测试用途
class GoodsAction extends CommonAction {
	private $Flash;

	public function __construct(){
		parent::__construct();
	   
	}

    public function index(){
		$id=$_GET['id'];	
		if(!empty($id)){
			$goods=M("Goods")->where("id='$id'")->find();
			$this->assign("goods",$goods);
		}
		$list=M("Goods_category")->select();
		$this->assign("list",$list);
		$this->display('./Tpl/Admin/Goods.html');
    }

    /**
     * 添加商品
     */
	public function add_goods(){
	
		import('ORG.Net.UploadFile');
		$upload = new UploadFile();// 实例化上传类
		$upload->maxSize  = 3145728 ;// 设置附件上传大小
		$upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
		$upload->savePath =  './Uploads/Goods/';// 设置附件上传目录
		if(!$upload->upload()) {// 上传错误提示错误信息
			if(empty($_POST['tupian'])){
			$this->error($upload->getErrorMsg());
			}else{
				$data['pic']=$_POST['tupian'];
			}
				
		}else{// 上传成功 获取上传文件信息
			$info =  $upload->getUploadFileInfo();
			$pic=$info[0]['savepath'].$info[0]['savename'];
			
			$data['pic']=ltrim($pic,".");
			
		}
		
		if(empty($_POST['title'])){
			$this->error("请输入商品名称");
		}
		if(empty($_POST['money'])){
			$this->error("请输入商品市场价格");
		}
		if(empty($_POST['money_new'])){
			$this->error("请输入商品会员价格");
		}
		if(empty($_POST['num'])){
			$this->error("请输入总库存");
		}
		if(empty($_POST['address'])){
			$this->error("请输入地址");
		}
		if(empty($_POST['tel'])){
			$this->error("请输入电话");
		}
		
		$data['rmb_bili']=$_POST['rmb_bili'];
		$data['cat_id']=$_POST['cat_id'];
		$data['title']=$_POST['title'];
		$data['money']=$_POST['money'];
		$data['money_new']=$_POST['money_new'];
		$data['num']=$_POST['num'];
		$data['address']=$_POST['address'];
		$data['tel']=$_POST['tel'];
		$data['status']=1;
		$data['add_time']=time();
		$id=intval($_POST['id']);
		if (!empty($id)) {
			$re=M('Goods')->where("id='$id'")->save($data);
		}else{
			$re=M("Goods")->add($data);
		}
		if($re){
			$this->success("添加成功","?s=Admin/Goods/goods_list");
		}else{
			$this->error("添加失败");
		}

	}
	public function goods_list(){
		
		$Goods = M('Goods'); // 实例化User对象
		import('ORG.Util.Page');// 导入分页类
		$count      = $Goods->count();// 查询满足要求的总记录数
		$Page       = new Page($count,10);// 实例化分页类 传入总记录数和每页显示的记录数
		$show       = $Page->show();// 分页显示输出
		// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
		$list = $Goods->order('add_time desc')->limit($Page->firstRow.','.$Page->listRows)->select();
		foreach ($list as $k=>$v){
			$gc=M("Goods_category")->where("id='{$v['cat_id']}'")->find();
			$list[$k]['cat_name']=$gc['name'];
		}
		$this->assign('list',$list);// 赋值数据集
		$this->assign('page',$show);// 赋值分页输出
		$this->display('./Tpl/Admin/Goods_list.html');
	}
	public function goods_del(){
		$id=intval($_GET['id']);
		$re=M("Goods")->where("id='$id'")->delete();
		if($re){
			$this->success("删除成功");
		}else{
			$this->error("删除失败");
		}
	}
	public function goods_status(){
		$id=intval($_GET['id']);
		$goods=M("Goods")->where("id='$id'")->find();
		if(empty($goods)){
			$this->error("无效商品记录");
		}
		if($goods['status']==0){
			$data['status']=1;
		}else{
			$data['status']=0;
		}
		$re=M("Goods")->where("id='$id'")->save($data);
		if($re){
			$this->success("商品状态已更新");
		}else{
			$this->error("商品状态更新失败");
		}
		
	}
	public function goods_position(){
		$id=intval($_GET['id']);
		$goods=M("Goods")->where("id='$id'")->find();
		if(empty($goods)){
			$this->error("无效商品记录");
		}
		if($goods['position']==0){
			$data['position']=1;
		}else{
			$data['position']=0;
		}
		$re=M("Goods")->where("id='$id'")->save($data);
		if($re){
			$this->success("商品位置已更新");
		}else{
			$this->error("商品位置更新失败");
		}
	
	}
	///********************产品类别*****************
	
	public function  goods_category_index(){
		$id=intval($_GET['id']);
		if(!empty($id)){
			$goods_category=M("Goods_category")->where("id='$id'")->find();
			
			$this->assign("goods_category",$goods_category);
		}
		$this->display('./Tpl/Admin/Goods_category_list.html');
		
	}
	public function goods_category_add(){
		$id=intval($_POST['id']);
		if(empty($_POST['name'])){
			$this->error("请输入类别名称");
		}
		$data['name']=$_POST['name'];
		if(!empty($id)){
			$re=M("Goods_category")->where("id='$id'")->save($data);
		}else{
			$re=M("Goods_category")->add($data);
		}
		if($re){
			$this->success("添加成功","?s=Admin/Goods/goods_category_list");
		}else{
			$this->error("添加失败");
		}
	}
	public function goods_category_list(){
		$Goods = M('Goods_category'); // 实例化User对象
		import('ORG.Util.Page');// 导入分页类
		$count      = $Goods->count();// 查询满足要求的总记录数
		$Page       = new Page($count,10);// 实例化分页类 传入总记录数和每页显示的记录数
		$show       = $Page->show();// 分页显示输出
		// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
		$list = $Goods->limit($Page->firstRow.','.$Page->listRows)->select();
		$this->assign('list',$list);// 赋值数据集
		$this->assign('page',$show);// 赋值分页输出
		$this->display('./Tpl/Admin/category_list.html');
	}
	
	public function category_del(){
		$id=intval($_GET['id']);
		$re=M("Goods_category")->where("id='$id'")->delete();
		if($re){
			$this->success("删除成功");
		}else{
			$this->error("删除失败");
		}
	}
	
	/**
	 * 购买商品列表
	 */
	public function goods_user_list(){
	
		$GU = M('Goods_user'); // 实例化User对象
		import('ORG.Util.Page');// 导入分页类
		$count      = $GU->where($where)->count();// 查询满足要求的总记录数
		$Page       = new Page($count,10);// 实例化分页类 传入总记录数和每页显示的记录数
		$show       = $Page->show();// 分页显示输出
		// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
		$field="t_goods_user.*,t_goods.pic,t_goods.title,t_goods.rmb_bili";
		$list = $GU->where($where)->field($field)->join("t_goods on t_goods_user.goods_id=t_goods.id")->order('t_goods.add_time desc')->limit($Page->firstRow.','.$Page->listRows)->select();
		foreach ($list as $k=>$v){
			$list[$k]['status_gu']=$this->gu_status($v['status']);
		}
		$this->assign('list',$list);// 赋值数据集
		$this->assign('page',$show);// 赋值分页输出
		$this->display('./Tpl/Admin/goods_user_list.html');
	}
	/**
	 * 格式化购买状态
	 */
	private function gu_status($status){
		switch ($status){
			case -1:$name="审核未通过";break;
			case 0:$name="未审核";break;
			case 1:$name="审核通过";break;
			default:$name="未知状态";
		}
		return $name;
	}
	/**
	 * 审核通过
	 */
	public function gu_pass(){
		$id=intval($_GET['id']);
		$gu=M("Goods_user")->where("id='$id'")->find();
		if(empty($gu)){
			$this->error("无效参数");
		}
		if($gu['status']!=0){
			$this->error("已审核，请勿重复审核");
		}
		$re=M("Goods_user")->where("id='$id'")->setField("status", 1);
		if($re){
			$this->success("审核通过","?s=Admin/Goods/goods_user_list");
		}else{
			$this->error("审核失败");
		}
		
	}
	/**
	 * 审核不通过
	 */
	public function gu_notpass(){
		$id=intval($_GET['id']);
		$gu=M("Goods_user")->where("id='$id'")->find();
		if(empty($gu)){
			$this->error("无效参数");
		}
		if($gu['status']!=0){
			$this->error("已审核，请勿重复审核");
		}
		$re=M("Goods_user")->where("id='$id'")->setField("status", -1);
		if($re){
			M("User")->where("id='{$gu['uid']}'")->setInc("rmb",$gu['rmb_money']);
			M("User")->where("id='{$gu['uid']}'")->setInc("xnb",$gu['xnb_money']);
			$this->success("审核不通过","?s=Admin/Goods/goods_user_list");
		}else{
			$this->error("审核失败");
		}
	}
}